package com.yulan.bankback.controller.bank;


import com.yulan.bankback.aop.Authorization;
import com.yulan.bankback.entity.model.bank.BankUser;
import com.yulan.bankback.entity.result.RetResponse;
import com.yulan.bankback.entity.result.RetResult;
import com.yulan.bankback.mapper.bank.BankUserMapper;
import com.yulan.bankback.service.bank.AliyunSmsService;
import com.yulan.bankback.service.bank.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private AliyunSmsService aliyunSmsService;

    @Resource
    private BankUserMapper bankUserMapper;

    private static final long oneDay = 60 * 60 * 1000 * 24L;

    @Authorization({"admin", "bank_user"})
    @PostMapping("/generate_msg/{phone}")
    public RetResult generateMsgCode(@PathVariable("phone") String phone){
        String code = redisService.get(phone + "_msg");
        if(StringUtils.isNotBlank(code))
            return RetResponse.makeErrRsp("请稍等一分钟再试");
        String newCode = this.generateCode();
        redisService.set(phone + "_msg", newCode, 60, TimeUnit.SECONDS);
        aliyunSmsService.sendMsg(phone, newCode);
        return RetResponse.makeOKRspMsg("发送成功");
    }

    @Authorization({"admin", "bank_user"})
    @PostMapping("/generate_msg_by_uid/{uid}")
    public RetResult generateMsgCodeByUid(@PathVariable("uid") String uid){
        if(StringUtils.isBlank(uid)) return RetResponse.makeErrRsp("uid不能为空");
        BankUser user = bankUserMapper.selectById(uid);
        if(user == null)
            return RetResponse.makeErrRsp("该用户不存在");
        String phone = user.getPhone();
        if(StringUtils.isBlank(phone))
            return RetResponse.makeErrRsp("该用户手机号不存在");

        String code = redisService.get(phone + "_msg");
        if(StringUtils.isNotBlank(code))
            return RetResponse.makeErrRsp("请稍等一分钟再试");
        String newCode = this.generateCode();
        redisService.set(phone + "_msg", newCode, 60, TimeUnit.SECONDS);
        aliyunSmsService.sendMsg(phone, newCode);
        return RetResponse.makeOKRspMsg("发送成功");
    }


    @Authorization({"admin", "bank_user"})
    @PostMapping("/generate_register_msg/{phone}")
    public RetResult generateRegisterMsgCode(@PathVariable("phone") String phone){
        String code = redisService.get(phone + "_register_msg");
        if(StringUtils.isNotBlank(code))
            return RetResponse.makeErrRsp("请稍等一分钟再试");
        String newCode = this.generateCode();
        redisService.set(phone + "_register_msg", newCode, 60, TimeUnit.SECONDS);
        aliyunSmsService.sendMsg(phone, newCode);
        return RetResponse.makeOKRspMsg("发送成功");
    }

    @Authorization({"admin", "bank_user"})
    @PostMapping("/verify_msg_code")
    public RetResult verifyMsgCode(String phone, String code){
        String oldCode = redisService.get(phone + "_msg");
        return StringUtils.equals(oldCode, code) ?
                RetResponse.makeOKRspMsg("验证成功") :
                RetResponse.makeOKRspMsg("验证失败");
    }

    @Authorization({"admin", "bank_user"})
    @PostMapping("/get_heart_beat")
    public RetResult getHeartBeat(String uid, Long interval){
        String useTime = redisService.hGetByKey("use_time_map",uid);
        if(useTime == null)
            redisService.hSet("use_time_map", uid, String.valueOf(interval), oneDay, TimeUnit.MILLISECONDS);
        else{
            long oldTime = Long.valueOf(useTime);
            long newTime = oldTime + interval;
            redisService.hSet("use_time_map", uid, String.valueOf(newTime), oneDay, TimeUnit.MILLISECONDS);
        }
        return RetResponse.makeOKRsp();
    }

    private String generateCode(){
        int res = 1;
        for(int i = 0; i<5; i++){
            res *= 10;
            res += new Random().nextInt(10);
        }
        return String.valueOf(res);
    }

}
